package com.commerceerp.system.service.Impl;

import com.alibaba.fastjson.JSON;
import com.commerceerp.system.mapper.GoodMapper;
import com.commerceerp.system.mapper.OFRecordMapper;
import com.commerceerp.system.mapper.OrderMapper;
import com.commerceerp.system.service.OFRecordService;
import com.commerceerp.system.util.CommonUtil;
import com.commerceerp.system.util.DateUtil;
import org.apache.bcel.generic.ObjectType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class OFRecordServiceImpl implements OFRecordService {

    @Autowired
    private OFRecordMapper ofRecordMapper;

    @Autowired
    private GoodMapper goodMapper;

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 获取已完成订单列表
     */
    @Override
    public List<Map> getAllOFRecord(String goodSystemSku, String beginTime, String endTime) {

        return ofRecordMapper.getAllOFRecord(goodSystemSku,beginTime,endTime);
    }

    /**
     * 获取已完成订单利润折线图
     */
    @Override
    public Map<String,Object> getProfitDiscount(String goodSystemSku, String sDate, String eDate) {

        sDate = DateUtil.getDefaultDate(sDate, eDate).get("sDate").toString();
        eDate = DateUtil.getDefaultDate(sDate, eDate).get("eDate").toString();

        List<Map>  profitList = ofRecordMapper.getProfitByDateGroup(goodSystemSku,sDate,eDate);
        List<Map>  omenoyList = ofRecordMapper.getOrderMoneyByDateGroup(goodSystemSku,sDate,eDate);
        List<Map>  onumberList = ofRecordMapper.getOrderNumberByDateGroup(goodSystemSku,sDate,eDate);

        List<Map<String,Object>> seriesList = new ArrayList<Map<String,Object>>();
        List<String> xAxisDataList = new ArrayList<String>();
        Map<String,Object> profitMap = new HashMap<String,Object>();
        Map<String,Object> omenoyMap = new HashMap<String,Object>();
        Map<String,Object> onumberMap = new HashMap<String,Object>();

        List<Double> profitDataList = new ArrayList<Double>();
        List<Double> omenoyDataList = new ArrayList<Double>();
        List<Integer> onumberDataList = new ArrayList<Integer>();

        for (Map<String,Object> tmp : profitList){
            xAxisDataList.add(tmp.get("tdate").toString());
            profitDataList.add(tmp.get("profit") == null ? 0.0 : Double.parseDouble(tmp.get("profit").toString()) );
        }
        for (Map<String,Object> tmp : omenoyList){
            omenoyDataList.add(tmp.get("omoney") == null ? 0.0 : Double.parseDouble(tmp.get("omoney").toString()) );
        }
        for (Map<String,Object> tmp : onumberList){
            onumberDataList.add(tmp.get("onumber") == null ? 0 : Integer.parseInt(tmp.get("onumber").toString()) );
        }

        profitMap.put("name","利润额");
        profitMap.put("data",profitDataList);
        omenoyMap.put("name","成交额");
        omenoyMap.put("data",omenoyDataList);
        onumberMap.put("name","订单数");
        onumberMap.put("data",onumberDataList);

        seriesList.add(profitMap);
        seriesList.add(omenoyMap);
        seriesList.add(onumberMap);

        Map<String,Object> returnMap = new HashMap<String,Object>();
        Map<String,Object> xAxisMap = new HashMap<String,Object>();
        Map<String,Object> yAxisMap1 = new HashMap<String,Object>();
        Map<String,Object> yAxisMap2 = new HashMap<String,Object>();
        List<Map> xAxisList = new ArrayList<Map>();
        List<Map> yAxisList = new ArrayList<Map>();
        xAxisMap.put("name","时间/天");
        xAxisMap.put("data",xAxisDataList);
        xAxisList.add(xAxisMap);

        yAxisMap1.put("name","数量/个");
        yAxisMap2.put("name","金额/元");
        yAxisList.add(yAxisMap1);
        yAxisList.add(yAxisMap2);

        returnMap.put("data",xAxisDataList);
        returnMap.put("series",seriesList);
        returnMap.put("xAxis",xAxisList);
        returnMap.put("yAxis",yAxisList);

        return returnMap;

    }


    /**
     * 获取商品sku的销量排行榜
     */
    @Override
    public List<Map> getAllFORecordSkuRate(String goodSystemSku, String beginTime, String endTime) {

        //获得sku信息
        List<Map> list = this.ofRecordMapper.getAllFORecordSkuRate(goodSystemSku,beginTime,endTime);
        //获得订单总数
        int count = ofRecordMapper.getAllOrderNum();
        for(Map map : list){
            //获取商品信息
            map.put("info",ofRecordMapper.getGoodInfo((String)map.get("order_sku")));
            //比例
            map.put("rate",((BigDecimal)map.get("num")).multiply(new BigDecimal(100)).divide(new BigDecimal(count),2,BigDecimal.ROUND_HALF_UP));
        }
        return list;
    }


    /**
     * 获取SKU成交时间分布图
     */
    @Override
    public Map<String,Object> getSkuOFNumber(String goodSystemSku, String beginTime, String endTime) {

        List<Map> list = this.ofRecordMapper.getSkuOFNumber(goodSystemSku,beginTime,endTime);
        List<String> allhour = CommonUtil.getAllHourByDay();
        List<Map> newData = new ArrayList<Map>();
        for(String hour : allhour){
             Map<String,Object> tmpMap = new HashMap<String,Object>();
             Boolean flag = false;
            for(Map<String,Object> tmp :list){

               if(hour.equals(tmp.get("hour").toString())){
                   tmpMap.put("hour",hour);
                   tmpMap.put("number",tmp.get("number"));
                   newData.add(tmpMap);
                   flag = true;
                   break;
               }
            }
            if(!flag){
                tmpMap.put("hour",hour);
                tmpMap.put("number",0);
                newData.add(tmpMap);
            }
        }
        List<Map<String,Object>> seriesList = new ArrayList<Map<String,Object>>();
        List<String> xAxisDataList = new ArrayList<String>();
        Map<String,Object> dataMap = new HashMap<String,Object>();

        List<Integer> numDataList = new ArrayList<Integer>();


        for (Map<String,Object> tmp : newData){
            xAxisDataList.add(tmp.get("hour").toString());
            numDataList.add(tmp.get("number") == null ? 0 : Integer.parseInt(tmp.get("number").toString()) );
        }

        dataMap.put("name","订单数");
        dataMap.put("data",numDataList);

        seriesList.add(dataMap);

        Map<String,Object> returnMap = new HashMap<String,Object>();
        Map<String,Object> xAxisMap = new HashMap<String,Object>();
        Map<String,Object> yAxisMap = new HashMap<String,Object>();
        List<Map> xAxisList = new ArrayList<Map>();
        List<Map> yAxisList = new ArrayList<Map>();
        xAxisMap.put("name","时间/时");
        xAxisMap.put("data",xAxisDataList);
        xAxisList.add(xAxisMap);

        yAxisMap.put("name","数量/个");
        yAxisList.add(yAxisMap);

        returnMap.put("data",xAxisDataList);
        returnMap.put("series",seriesList);
        returnMap.put("xAxis",xAxisList);
        returnMap.put("yAxis",yAxisList);

        return returnMap;
    }


    /**
     *成交价分析-汇总
     */
    @Override
    public List<Map> getOFRGoodPriceCollect(String goodSystemSku, String beginTime, String endTime) {

        List<Map> list = this.ofRecordMapper.getOFRGoodPriceCollect(goodSystemSku,beginTime,endTime);

        for(Map<String,Object> map : list){

            if(map.get("goodPrice") != null){
                map.put("goodPrice",map.get("goodPrice").toString());
            }
        }

        return list;
    }


    /**
     *成交价分析-明细
     */
    @Override
    public List<Map> getOFRGoodPriceDetail(String goodSystemSku, String beginTime, String endTime, String goodPrice) {

        List<Map> list = this.ofRecordMapper.getOFRGoodPriceDetail(goodSystemSku,beginTime,endTime,goodPrice);

        return list;
    }


    /**
     * 成交价扇形图
     */
    @Override
    public Map<String, Object> getOFRPriceRate(String goodSystemSku, String beginTime, String endTime) {

        List<Map> list = this.ofRecordMapper.getOFRPriceRate(goodSystemSku,beginTime,endTime);

        List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();

        List<String> legendDataList = new ArrayList<String>();

        for(Map<String,Object> tmp : list){

            legendDataList.add(tmp.get("orderGoodPrice").toString());

            Map<String,Object> dataMap = new HashMap<String,Object>();
            dataMap.put("name",tmp.get("orderGoodPrice").toString());
            Double Rate = new BigDecimal(tmp.get("rate").toString()).multiply(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            dataMap.put("value",Rate);
            dataList.add(dataMap);
        }

        Map<String,Object> seriesMap = new HashMap<String, Object>();
        seriesMap.put("name","成交价占比图");
        seriesMap.put("data",dataList);

        Map<String,Object> returnMap = new HashMap<String, Object>();
        returnMap.put("legend",legendDataList);
        returnMap.put("series",seriesMap);

        return  returnMap;

    }


    /**
     * 客户分布排行榜
     */
    @Override
    public List<Map> getUserDistribution(String goodSystemSku, String beginTime, String endTime) {

        List<Map> list = this.ofRecordMapper.getUserDistribution(goodSystemSku,beginTime,endTime);
        int i = 0;
        for(Map<String,Object> map :list){
            i++;
            map.put("rm",i);
            map.put("money",new BigDecimal(map.get("money").toString()).setScale(2,BigDecimal.ROUND_HALF_UP));

        }

        return list;
    }


    /**
     * 客户分布扇形图
     * @param goodSystemSku
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public Map<String, Object> getUserDistributionRate(String goodSystemSku, String beginTime, String endTime) {

        int count = ofRecordMapper.getAllOrderNum();
        List<Map> list = this.ofRecordMapper.getUserDistributionRate(count,goodSystemSku,beginTime,endTime);
        List<Map> newDataList = new ArrayList<Map>();
        Map<String,Object> otherMap = new HashMap<String,Object>();
        double otherRate  = 0.0;
        for(Map<String,Object> map :list){
           newDataList.add(map);
           otherRate =otherRate + Double.valueOf(map.get("rate").toString());
        }
        otherMap.put("city","other");
        otherMap.put("rate",1 - otherRate);
        newDataList.add(otherMap);
        List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
        List<String> legendDataList = new ArrayList<String>();
        for(Map<String,Object> tmp : newDataList){
            legendDataList.add(tmp.get("city").toString());
            Map<String,Object> dataMap = new HashMap<String,Object>();
            dataMap.put("name",tmp.get("city").toString());
            Double Rate = new BigDecimal(tmp.get("rate").toString()).multiply(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            dataMap.put("value",Rate);
            dataList.add(dataMap);
        }

        Map<String,Object> seriesMap = new HashMap<String, Object>();
        seriesMap.put("name","客户分布图");
        seriesMap.put("data",dataList);

        Map<String,Object> returnMap = new HashMap<String, Object>();
        returnMap.put("legend",legendDataList);
        returnMap.put("series",seriesMap);

        return  returnMap;

    }



    /**
     * 获取下拉框商品sku
     */
    @Override
    public List<Map> getAllSystemGoodSKu() {


        return goodMapper.getAllSystemGoodSKu();
    }


    /**
     * 获取最近6天成交额和未成交额
     * @return
     */
    @Override
    public Map<String, Object> getOrderMoneyDiscount() {

        List<String> dataList = DateUtil.getBeforeBetweenSevenDay(DateUtil.getYMDDate());

        String beginTime = DateUtil.getDateBeforeSixDay(DateUtil.getYMDDate());

        List<Map> fMap = this.orderMapper.getNumAndMoneyByFinishOrder(beginTime);
        List<Map> CMap = this.orderMapper.getNumAndMoneyByCancelOrder(beginTime);

        List<Map<String,Object>> seriesList = new ArrayList<Map<String,Object>>();
        List<String> xAxisDataList = new ArrayList<String>();
        Map<String,Object> fOrderMap = new HashMap<String,Object>();
        Map<String,Object> cOrderMap = new HashMap<String,Object>();

        List<Double> fOrderDataList = new ArrayList<Double>();
        List<Double> cOrderDataList = new ArrayList<Double>();

        xAxisDataList.addAll(dataList);

            for(String date : dataList){
                Boolean flag = false;
                for (Map<String,Object> tmp : fMap) {
                  if(date.equals(tmp.get("tdate").toString())){
                      fOrderDataList.add(tmp.get("money") == null ? 0.0 : new BigDecimal(tmp.get("money").toString()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue() );
                      flag = true;
                      break;
                  }
                }
                if(!flag){
                    fOrderDataList.add( 0.0 );
                }
            }


        for(String date : dataList){
            Boolean flag = false;
            for (Map<String,Object> tmp : CMap) {
                if(date.equals(tmp.get("tdate").toString())){
                    cOrderDataList.add(tmp.get("money") == null ? 0.0 : new BigDecimal(tmp.get("money").toString()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
                    flag = true;
                    break;
                }
            }
            if(!flag){
                cOrderDataList.add( 0.0 );
            }
        }

        fOrderMap.put("name","成交额");
        fOrderMap.put("data",fOrderDataList);
        cOrderMap.put("name","未成交额");
        cOrderMap.put("data",cOrderDataList);

        seriesList.add(fOrderMap);
        seriesList.add(cOrderMap);

        Map<String,Object> returnMap = new HashMap<String,Object>();
        Map<String,Object> xAxisMap = new HashMap<String,Object>();
        Map<String,Object> yAxisMap = new HashMap<String,Object>();
        List<Map> xAxisList = new ArrayList<Map>();
        List<Map> yAxisList = new ArrayList<Map>();
        xAxisMap.put("name","时间/天");
        xAxisMap.put("data",xAxisDataList);
        xAxisList.add(xAxisMap);

        yAxisMap.put("name","金额/元");
        yAxisList.add(yAxisMap);

        returnMap.put("data",xAxisDataList);
        returnMap.put("series",seriesList);
        returnMap.put("xAxis",xAxisList);
        returnMap.put("yAxis",yAxisList);

        return returnMap;

    }



    /**
     * 获取最近6天未成交订单数和未成交订单数
     * @return
     */
    @Override
    public Map<String, Object> getOrderNumDiscount() {

        List<String> dataList = DateUtil.getBeforeBetweenSevenDay(DateUtil.getYMDDate());

        String beginTime = DateUtil.getDateBeforeSixDay(DateUtil.getYMDDate());

        List<Map> fMap = this.orderMapper.getNumAndMoneyByFinishOrder(beginTime);
        List<Map> CMap = this.orderMapper.getNumAndMoneyByCancelOrder(beginTime);

        List<Map<String,Object>> seriesList = new ArrayList<Map<String,Object>>();
        List<String> xAxisDataList = new ArrayList<String>();
        Map<String,Object> fOrderMap = new HashMap<String,Object>();
        Map<String,Object> cOrderMap = new HashMap<String,Object>();

        List<Integer> fOrderDataList = new ArrayList<Integer>();
        List<Integer> cOrderDataList = new ArrayList<Integer>();

        xAxisDataList.addAll(dataList);

        for(String date : dataList){
            Boolean flag = false;
            for (Map<String,Object> tmp : fMap) {
                if(date.equals(tmp.get("tdate").toString())){
                    fOrderDataList.add(tmp.get("cnt") == null ? 0 : Integer.parseInt(tmp.get("cnt").toString()));
                    flag = true;
                    break;
                }
            }
            if(!flag){
                fOrderDataList.add(0);
            }
        }

        for(String date : dataList){
            Boolean flag = false;
            for (Map<String,Object> tmp : CMap) {
                if(date.equals(tmp.get("tdate").toString())){
                    cOrderDataList.add(tmp.get("cnt") == null ? 0 : Integer.parseInt(tmp.get("cnt").toString()));
                    flag = true;
                    break;
                }
            }
            if(!flag){
                cOrderDataList.add( 0 );
            }
        }


        fOrderMap.put("name","成交订单数");
        fOrderMap.put("data",fOrderDataList);
        cOrderMap.put("name","未成交订单数");
        cOrderMap.put("data",cOrderDataList);

        seriesList.add(fOrderMap);
        seriesList.add(cOrderMap);

        Map<String,Object> returnMap = new HashMap<String,Object>();
        Map<String,Object> xAxisMap = new HashMap<String,Object>();
        Map<String,Object> yAxisMap = new HashMap<String,Object>();
        List<Map> xAxisList = new ArrayList<Map>();
        List<Map> yAxisList = new ArrayList<Map>();
        xAxisMap.put("name","时间/天");
        xAxisMap.put("data",xAxisDataList);
        xAxisList.add(xAxisMap);

        yAxisMap.put("name","数量/个");
        yAxisList.add(yAxisMap);

        returnMap.put("data",xAxisDataList);
        returnMap.put("series",seriesList);
        returnMap.put("xAxis",xAxisList);
        returnMap.put("yAxis",yAxisList);

        return returnMap;

    }




}
